using System;
using WB.Mvc.Forms.Widgets;

namespace WB.Mvc.Forms
{
	public class EnumField : Field
	{
		private readonly Type enumType;

		public EnumField(string name, Type enumType) : base(name)
		{
			this.enumType = enumType;
			if (!enumType.IsEnum)
				throw new ArgumentException("T must be an Enum.");
		}

		/// <summary>
		/// Gets the widget used to render the input for this field.
		/// </summary>
		public override Widget GetWidget()
		{
			var widget = new RadioButtonList(Name);

			string[] names = Enum.GetNames(enumType);

			foreach (string name in names)
			{
				var val = (int) Enum.Parse(enumType, name);
				widget.ListItems.Add(new ListItem {Text = name, Value = val.ToString()});
			}

			return widget;
		}
	}
}